﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using ExtAspNet;
using MyDMS.DAL;

namespace MyDMS_ExtJS
{
    public class PageBase : Page
    {
        protected static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private const string RootPath = "/MyDMS";
        public static Author Author;
        private static readonly MyDMSEntity MyDmsEntity = new MyDMSEntity();

        protected string WebTitle;

        public PageBase()
        {
            this.Load += new EventHandler(PageBase_Load);
        }

        void PageBase_Load(object sender, EventArgs e)
        {
            WebTitle = System.Configuration.ConfigurationSettings.AppSettings["WebSite_Title"];
            if (Session["Author"] != null)
            {
                Author = (Author) Session["Author"];
            }
            else
            {
                string redirectUrl = "~/Default.aspx";
                Response.Redirect(redirectUrl);
            }
        }

        #region Grid Related

        /// <summary>
        /// 选中的行
        /// </summary>
        /// <param name="grid"></param>
        /// <returns></returns>
        protected string HowManyRowsAreSelected(Grid grid)
        {
            StringBuilder sb = new StringBuilder();
            int selectedCount = grid.SelectedRowIndexArray.Length;
            if (selectedCount > 0)
            {
                sb.AppendFormat("共选中了 {0} 行：", selectedCount);
                sb.Append("<ol class=\"result\">");
                /*
                sb.Append("<tr><th></th>");
                foreach (string datakey in grid.DataKeyNames)
                {
                    sb.AppendFormat("<th>{0}</th>", datakey);
                }
                sb.Append("</tr>");
                 * */

                for (int i = 0; i < selectedCount; i++)
                {
                    int rowIndex = grid.SelectedRowIndexArray[i];
                    sb.Append("<li><ul>");

                    sb.AppendFormat("<li>Index：{0}</li>", rowIndex + 1);

                    // 如果是内存分页，所有分页的数据都存在，rowIndex 就是在全部数据中的顺序，而不是当前页的顺序
                    if (grid.AllowPaging && !grid.IsDatabasePaging)
                    {
                        rowIndex = grid.PageIndex * grid.PageSize + rowIndex;
                    }

                    object[] dataKeys = grid.DataKeys[rowIndex];
                    for (int j = 0; j < dataKeys.Length; j++)
                    {
                        sb.AppendFormat("<li>{0}：{1}</li>", grid.DataKeyNames[j], dataKeys[j]);
                    }
                    /*
                    foreach (object key in grid.DataKeys[rowIndex])
                    {
                        sb.AppendFormat("<li>{0}：{1}</li>", grid.DataKeyNames[rowIndex], key);
                    }
                     * */
                    sb.Append("</ul></li>");
                }
                sb.Append("</ol>");
            }
            else
            {
                sb.Append("<strong>没有选中任何一行！</strong>");
            }

            return sb.ToString();
        }

        /// <summary>
        /// 获取性别的字面值，在 ASPX 中调用
        /// </summary>
        /// <param name="gender"></param>
        /// <returns></returns>
        protected string GetGender(object gender)
        {
            if (Convert.ToInt32(gender) == 1)
            {
                return "男";
            }
            else
            {
                return "女";
            }
        }
        #endregion

    }
}